1260C - Infinite Fence - CodeForces Solution


greedy math number theory *1700

Please click on ads to support us..

Python Code:

n = int(input())
from math import gcd, ceil
for _ in range(n):
    r, b, k = map(int, input().split())
    g = gcd(r, b)
    r /= g
    b /= g
    if r == b:
        print("OBEY")
        continue
    if r > b:
        r, b = b, r
    fit = ceil((b-1) / r)
    if fit < k:
        print("OBEY")
    else:
        print("REBEL")

C++ Code:

#include<bits/stdc++.h>
using namespace std;
#define FOR(i, j, k, in) for (ll i = j; i < k; i += in)
#define RFOR(i, j, k, in) for (ll i = j; i >= k; i -= in)
#define REP(i, j) FOR(i, 0, j, 1)
#define RREP(i, j) RFOR(i, j, 0, 1)
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
#define pb push_back
#define f(v) v.begin(),v.end() 

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<bool> vb;
typedef vector<vector<bool>> vvb;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<string, string> pss;
typedef unordered_map<int, int> umap_ii;
typedef map<int, int> mii;
ll modAdd( ll a , ll b ){return ( a%MOD + b%MOD ) % MOD;}
ll modSubtract( ll a , ll b ){return ( ( a%MOD - b%MOD ) + MOD )%MOD;}
ll modProduct( ll a , ll b ){return ( a%MOD * b%MOD ) % MOD;}
ll binPow( ll a , ll b ){if(b==0)return 1; ll res = binPow(a,b/2);if(b&1ll)res=res*a;return res;}
ll inv(ll x){ return binPow(x, MOD-2);}
ll divide(ll x, ll y){return modProduct(x, inv(y));}
void printVector(vector<long long> v) {for(long long i=0; i<v.size(); i++) cout<<v[i]<<" "; cout<<endl;}
void printMatrix(vector<vector<ll>> v) {for(ll i=0; i<v.size(); i++){for(ll j=0; j<v[i].size(); j++) cout<<v[i][j]<<" "; cout<<endl;}}
void printPairVector(vector<pair<ll,ll>> v){for(ll i=0; i<v.size(); i++) cout<<v[i].first<<","<<v[i].second<<" "; cout<<endl;}
void printGraph(vvl& adj, ll n){for(ll i=1; i<=n; i++){cout<<i<<" -> ";for(ll j=0; j<adj[i].size(); j++){cout<<adj[i][j]<<" ";}cout<<endl;} cout<<endl;}
void inputUndirectedGraph(vvl& adj, ll n, ll m){for(ll i=0; i<m; i++){ll u,v; cin>>u>>v; adj[u].push_back(v);adj[v].push_back(u);}}
void inputDirectedGraph(vvl& adj, ll n, ll m){for(ll i=0; i<m; i++){ll u,v; cin>>u>>v; adj[u].push_back(v);}}
void inputVector(vl& v){for(ll i=0; i<v.size(); i++){cin>>v[i];}}
ll spacesplit(string s){ll i=0; while(i<s.length() && s[i]!=' '){i++;} return i;}
 
 
ll maxn;
vl segt;
vb marked;
void push(int v) {
if (marked[v]) {
segt[v*2] = segt[v*2+1] = segt[v];
marked[v*2] = marked[v*2+1] = true;
marked[v] = false;
}
}
 
void update(ll v, ll tl, ll tr, ll l, ll r, ll new_val) {
if (l > r)
return;
if (l == tl && tr == r) {
segt[v] = new_val;
marked[v] = true;
} else {
push(v);
ll tm = (tl + tr) / 2;
update(v*2, tl, tm, l, min(r, tm), new_val);
update(v*2+1, tm+1, tr, max(l, tm+1), r, new_val);
}
}
 
 
ll get(ll v, ll tl, ll tr, ll pos) {
if (tl == tr) {
return segt[v];
}
push(v);
ll tm = (tl + tr) / 2;
if (pos <= tm)
return get(v*2, tl, tm, pos);
else
return get(v*2+1, tm+1, tr, pos);
}
 
void build(ll v, ll tl, ll tr, vl& a)
{
    if(tl==tr)
    {
        segt[v] = a[tl];
        return;
    }
    ll tm = (tl+tr)>>1;
    build(2*v, tl, tm, a);
    build(2*v+1, tm+1, tr, a);
    segt[v] = min(segt[2*v], segt[2*v+1]);
}
 
ll query(ll v, ll tl, ll tr, ll l, ll r)
{
    if(l>r)
        return LLONG_MAX;
    if(l==tl && r==tr)
        return segt[v];
    ll tm = (tl+tr)>>1;
    return min(query(2*v, tl, tm, l, min(r, tm)), query(2*v+1, tm+1, tr, max(l, tm+1), r));
}
 
void update(ll pos, ll x, ll v, ll tl, ll tr)
{
    if(tl==tr)
    {
        segt[v] = x;
        return;
    }
    ll tm = (tl+tr)>>1;
    if(pos <= tm)
    {
        update(pos, x, 2*v, tl, tm);
    }
    else
    {
        update(pos, x, 2*v+1, tm+1, tr);
    }
    segt[v] = min(segt[2*v], segt[2*v+1]);
}

ll gcd(ll a, ll b, ll& x, ll& y) {
    if (b == 0) {
        x = 1;
        y = 0;
        return a;
    }
    ll x1, y1;
    ll d = gcd(b, a % b, x1, y1);
    x = y1;
    y = x1 - y1 * (a / b);
    return d;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll t;
    cin>>t;
    while(t--)
    {
        ll r, b, k;
        cin>>r>>b>>k;

        if(r>b)
            swap(r, b);
        ll g = __gcd(r, b);
        r /= g;
        b /= g;
        // int sr = 1, sb = -1;

        // ll x, y;
        // gcd(r, -b, x, y);

        // ll pos = b*y;
        
        if((k-1)*r+1>=b)
            cout<<"OBEY"<<endl;
        else
            cout<<"REBEL"<<endl;
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

946. Validate Stack Sequences
921. Minimum Add to Make Parentheses Valid
881. Boats to Save People
497. Random Point in Non-overlapping Rectangles
528. Random Pick with Weight
470. Implement Rand10() Using Rand7()
866. Prime Palindrome
1516A - Tit for Tat
622. Design Circular Queue
814. Binary Tree Pruning
791. Custom Sort String
787. Cheapest Flights Within K Stops
779. K-th Symbol in Grammar
701. Insert into a Binary Search Tree
429. N-ary Tree Level Order Traversal
739. Daily Temperatures
647. Palindromic Substrings
583. Delete Operation for Two Strings
518. Coin Change 2
516. Longest Palindromic Subsequence
468. Validate IP Address
450. Delete Node in a BST
445. Add Two Numbers II
442. Find All Duplicates in an Array
437. Path Sum III
436. Find Right Interval
435. Non-overlapping Intervals
406. Queue Reconstruction by Height
380. Insert Delete GetRandom O(1)
332. Reconstruct Itinerary